<template>
  <div class="test-scoring-api">
    <h2>评分系统API测试</h2>
    
    <!-- 参数设置区域 -->
    <div class="params-section">
      <h3>测试参数设置</h3>
      <div class="param-item">
        <label>作业ID：</label>
        <input v-model.number="testParams.jobId" type="number" min="1" />
      </div>
      <div class="param-item">
        <label>提交ID：</label>
        <input v-model.number="testParams.submissionId" type="number" min="1" />
      </div>
    </div>

    <!-- 测试按钮区域 -->
    <div class="test-buttons">
      <h3>评分标准管理</h3>
      <div class="button-group">
        <button @click="testGetScoringCriteria">获取评分标准</button>
        <button @click="testCreateScoringCriteria">创建评分标准</button>
        <button @click="testUpdateScoringCriteria">更新评分标准</button>
        <button @click="testDeleteScoringCriteria">删除评分标准</button>
      </div>

      <h3>评分操作</h3>
      <div class="button-group">
        <button @click="testGetPendingSubmissions">获取待评分列表</button>
        <button @click="testSubmitScores">提交评分</button>
        <button @click="testUpdateScores">更新评分</button>
        <button @click="testGetScoreHistory">获取评分历史</button>
        <button @click="testGetScoreStatistics">获取评分统计</button>
      </div>
    </div>

    <!-- 结果显示区域 -->
    <div class="result-section" v-if="testResult">
      <h3>测试结果</h3>
      <div class="result-status" :class="{ success: testResult.success, error: !testResult.success }">
        {{ testResult.message }}
      </div>
      <div class="result-data" v-if="testResult.data">
        <pre>{{ JSON.stringify(testResult.data, null, 2) }}</pre>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getScoringCriteria,
  createScoringCriteria,
  updateScoringCriteria,
  deleteScoringCriteria,
  getPendingSubmissions,
  submitScores,
  updateScores,
  getScoreHistory,
  getScoreStatistics
} from '@/api/api';

export default {
  name: 'TestScoringAPI',
  
  data() {
    return {
      testParams: {
        jobId: 1,
        submissionId: 1
      },
      testResult: null
    }
  },

  methods: {
    // 通用结果处理方法
    handleSuccess(data, message = '请求成功') {
      this.testResult = {
        success: true,
        message,
        data
      };
    },

    handleError(error) {
      this.testResult = {
        success: false,
        message: error.response?.data?.message || error.message,
        data: error.response?.data
      };
      console.error('API请求失败:', error);
    },

    // 1. 评分标准管理测试方法
    async testGetScoringCriteria() {
      try {
        const data = await getScoringCriteria(this.testParams.jobId);
        this.handleSuccess(data, '获取评分标准成功');
      } catch (error) {
        this.handleError(error);
      }
    },

    async testCreateScoringCriteria() {
      const testData = {
        jobId: this.testParams.jobId,
        criteria: [
          {
            name: "完整性",
            description: "答案是否完整覆盖所有要求",
            weight: 30,
            scoreType: "star",
            minScore: 1,
            maxScore: 5
          },
          {
            name: "创新性",
            description: "答案是否具有创新性思维",
            weight: 70,
            scoreType: "slider",
            minScore: 0,
            maxScore: 100
          }
        ]
      };

      try {
        const data = await createScoringCriteria(testData);
        this.handleSuccess(data, '创建评分标准成功');
      } catch (error) {
        this.handleError(error);
      }
    },

    async testUpdateScoringCriteria() {
      const testData = {
        name: "完整性（更新）",
        description: "答案是否完整覆盖所有要求（更新）",
        weight: 40,
        scoreType: "star",
        minScore: 1,
        maxScore: 5
      };

      try {
        const data = await updateScoringCriteria(this.testParams.jobId, 1, testData);
        this.handleSuccess(data, '更新评分标准成功');
      } catch (error) {
        this.handleError(error);
      }
    },

    async testDeleteScoringCriteria() {
      try {
        const data = await deleteScoringCriteria(this.testParams.jobId, 1);
        this.handleSuccess(data, '删除评分标准成功');
      } catch (error) {
        this.handleError(error);
      }
    },

    // 2. 评分操作测试方法
    async testGetPendingSubmissions() {
      try {
        const data = await getPendingSubmissions(this.testParams.jobId, {
          page: 1,
          size: 10,
          status: 'PENDING'
        });
        this.handleSuccess(data, '获取待评分列表成功');
      } catch (error) {
        this.handleError(error);
      }
    },

    async testSubmitScores() {
      const testData = {
        scores: [
          {
            criteriaId: 1,
            score: 4.5,
            comment: "回答比较完整，但还有改进空间"
          },
          {
            criteriaId: 2,
            score: 85,
            comment: "创新性思维突出"
          }
        ]
      };

      try {
        const data = await submitScores(this.testParams.submissionId, testData);
        this.handleSuccess(data, '提交评分成功');
      } catch (error) {
        this.handleError(error);
      }
    },

    async testUpdateScores() {
      const testData = {
        scores: [
          {
            criteriaId: 1,
            score: 5,
            comment: "回答非常完整（更新）"
          },
          {
            criteriaId: 2,
            score: 90,
            comment: "创新性思维非常突出（更新）"
          }
        ]
      };

      try {
        const data = await updateScores(this.testParams.submissionId, testData);
        this.handleSuccess(data, '更新评分成功');
      } catch (error) {
        this.handleError(error);
      }
    },

    async testGetScoreHistory() {
      try {
        const data = await getScoreHistory(this.testParams.submissionId);
        this.handleSuccess(data, '获取评分历史成功');
      } catch (error) {
        this.handleError(error);
      }
    },

    async testGetScoreStatistics() {
      try {
        const data = await getScoreStatistics(this.testParams.jobId);
        this.handleSuccess(data, '获取评分统计成功');
      } catch (error) {
        this.handleError(error);
      }
    }
  }
}
</script>

<style scoped>
.test-scoring-api {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

h2, h3 {
  color: #333;
  margin-bottom: 20px;
}

.params-section {
  background: #f5f5f5;
  padding: 20px;
  border-radius: 8px;
  margin-bottom: 20px;
}

.param-item {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.param-item label {
  width: 80px;
  margin-right: 10px;
}

.param-item input {
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
  width: 100px;
}

.test-buttons {
  margin-bottom: 20px;
}

.button-group {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 20px;
}

button {
  padding: 8px 16px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
}

button:hover {
  background-color: #45a049;
}

.result-section {
  background: #f9f9f9;
  padding: 20px;
  border-radius: 8px;
}

.result-status {
  padding: 10px;
  border-radius: 4px;
  margin-bottom: 10px;
}

.result-status.success {
  background-color: #dff0d8;
  color: #3c763d;
}

.result-status.error {
  background-color: #f2dede;
  color: #a94442;
}

.result-data {
  background: #fff;
  padding: 15px;
  border-radius: 4px;
  border: 1px solid #ddd;
  overflow-x: auto;
}

.result-data pre {
  margin: 0;
  white-space: pre-wrap;
  word-wrap: break-word;
}
</style> 